|
Download
FAQ History |
|
API
Search Feedback |
Web Service Endpoints
A Web service client can access J2EE applications in two ways. First, the client can access a Web service created with JAX-RPC. Behind the scenes, JAX-RPC uses a servlet to implement the Web service. (For more information on JAX-RPC, see the chapter, Building Web Services With JAX-RPC.) Second, a Web service client can access a stateless session bean through the service endpoint interface of the bean. Other types of enterprise beans cannot be accessed by Web service clients.
Provided that it uses the correct protocols (SOAP, HTTP, WSDL), any Web service client can access a stateless session bean, whether or not the client is written in the Java programming language. The client doesn't even "know" what technology implements the service--stateless session bean, JAX-RPC, or some other technology. Because of this flexibility, you can integrate J2EE applications with Web services.
Web Service Example: HelloServiceEJB
The files for this example are in the
<INSTALL>/j2eetutorial14/examples/ejb/helloservice/directory.
HelloServiceEJBis a stateless session bean that implements a single method,sayHello. This method matches thesayHelloinvoked by the clients described in Creating Web Service Clients with JAX-RPC. Later in this section, you'll test theHelloServiceEJBby running one of these JAX-RPC clients.Source Code for HelloServiceEJB
The source files for this example,
HelloService.javaandHelloServiceBean.java, are located in thehelloservice/src/subdirectory.Web Service Endpoint Interface
HelloIFis the bean's Web service endpoint interface. It provides the client's view of the Web service, hiding the stateless session bean from the client. A Web service endpoint interface must conform to the rules of a JAX-RPC service endpoint interface. For a summary of those rules, see Coding the Service Endpoint Interface and Implementation Class. Here is the source code for theHelloIFinterface:package helloservice; import java.rmi.RemoteException; import java.rmi.Remote; public interface HelloIF extends Remote { public String sayHello(String name) throws RemoteException; }Stateless Session Bean Implementation Class
The
HelloServiceBeanclass implements thesayHellomethod defined by theHelloServiceinterface. The interface decouples the implementation class from the type of client access. For example, if you added remote and home interfaces toHelloServiceEJB, the methods of theHelloServiceBeanclass could also be accessed by remote clients that are J2EE components. No changes to theHelloServiceBeanclass would be necessary. The source code for theHelloServiceBeanclass follows:package helloservice; import java.rmi.RemoteException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class HelloServiceBean implements SessionBean { public String sayHello(String name) { return "Hello " + name + "from HelloServiceEJB"; } public HelloServiceBean() {} public void ejbCreate() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sc) {} }Building HelloServiceEJB
In a terminal window, go to the
<INSTALL>/j2eetutorial14/examples/ejb/helloservice/directory. To buildHelloServiceEJB, type the following command:This command performs the following tasks:
The
wscompiletool writes theMyHelloService.wsdlfile to thehelloservice/build/subdirectory. For more information about thewscompiletool, see Building Web Services With JAX-RPC.Use
deploytoolto package and deploy this example.Creating the Application
In this section, you'll create a J2EE application named
HelloService, storing it in the fileHelloService.ear.
- In
deploytool, select FileNew
Application EAR.
- Click Browse.
- In the file chooser, navigate to
<INSTALL>/j2eetutorial14/examples/ejb/helloservice/.- In the File Name field, enter
HelloService.ear.- Click New Application.
- Click OK.
- Verify that the
HelloService.earfile resides in<INSTALL>/j2eetutorial14/examples/ejb/helloservice/.Packaging the Enterprise Bean
Start the Edit Enterprise Bean wizard by selecting File
New
Enterprise JavaBean JAR. The wizard displays the following dialog boxes.
- Introduction dialog box
- EJB JAR dialog box
- Select the button labelled Create New JAR Module in Application.
- In the combo box below this button, select
HelloService.- In the JAR Display Name field, enter
HelloServiceJAR.- Click Edit.
- In the tree under Available Files, locate the
<INSTALL>/j2eetutorial14/examples/ejb/helloservice/build/directory.- In the Available Files tree select
mapping.xmlandMyHelloService.wsdl.- Select these classes from the
.../helloservice/build/helloservice/directory:HelloIF.classandHelloServiceBean.class.- Click Add.
- Click OK.
- Click Next.
- General dialog box
- In the Configuration Options dialog box, click Next. The wizard will automatically select the Yes button for Expose Bean as Web Service Endpoint.
- In the Choose Service dialog box:
- In the Web Service Endpoint dialog box:
- In the Review Settings dialog box:
Deploying the Enterprise Application
Now that the J2EE application contains the enterprise bean, it is ready for deployment.
Building the Web Service Client
In the next section, to test the Web service implemented by
HelloServiceEJB, you will run the JAX-RPC client described in Building Web Services With JAX-RPC.To build the static stub client, perform these steps:
For details about creating the JAX-RPC service and client, see these sections: Creating a Web Service with JAX-RPC and Static Stub Client Example.
Running the Web Service Client
Verify that the
HelloServiceEJBhas been deployed by clicking on the target application server in the Servers tree indeploytool. In the Deployed Objects tree you should see HelloService.To run the client, go to the
<INSTALL>/j2eetutorial14/examples/jaxrpc/staticstub/directory and enter:The client should display the following line:
|
Download
FAQ History |
|
API
Search Feedback |
All of the material in The J2EE 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.